package com.object;

public class StringUtil {
    //构造函数
    public StringUtil(){}
    //封装一个遍历字符串的操作信息
    public void StringMap(String str){
        if(str.length()<=0){
            System.out.println("字符串是空的");
        }

        for(int i = 0; i<str.length();i++){
            if(i== str.length()-1){
                System.out.print(str.charAt(i));
            }else{
                System.out.print(str.charAt(i)+", ");
            }

        }
    }

    //统计字符串中每个字符出现的次数
    public void StringComputed(String str){
        if(str.length()<=0){
            System.out.println("字符串是空的");
        }
        //定义初始化的变量的值
        int caseUpper=0,caseLower=0,num=0;

        for (int i =0;i<str.length();i++){
            if(str.charAt(i) >= 'A' && str.charAt(i)<='Z'){
                caseUpper+=1;
            }

            if(str.charAt(i) >= 'a' && str.charAt(i)<='z'){
                caseLower+=1;
            }

            if(str.charAt(i) >= '0' && str.charAt(i)<='9'){
                num+=1;
            }
        }

        System.out.print("大写字母:"+caseUpper+",小写字母:"+caseLower+",数字:"+num);

    }

    //拼接字符串内容
    public String ArrayToString(int[] arr){
        StringBuilder string = new StringBuilder();
        string.append("[");
        for(int i = 0;i<arr.length;i++){
            if(i== arr.length-1){
                string.append(arr[i]);
            }else{
                string.append(arr[i]).append(",");
            }
        }
        string.append("]");

        return string.toString();
    }

    //字符串反转
    public String StringReverse(String str){
        if(str.length()<=0){
            System.out.println("字符串是空的");
        }

        StringBuilder string = new StringBuilder(str);


        return string.reverse().toString();
    }
}
